package sundun.sharding.aspect;

import org.apache.shardingsphere.infra.hint.HintManager;
import sundun.sharding.aspect.base.Callable;

/**
 * Hint策略工具类
 * @author: zxl
 * @date: 2024-07-24 14:26
 **/
public class ShardingHintJdbcUtils {
    public static <T> T hitMasterOnly(Callable.Func0<T> action){
        try(HintManager hintManager=HintManager.getInstance()){
            hintManager.setWriteRouteOnly();
            return action.invoke();
        }
    }

    public static <T> T hitSlaveOnly(Comparable<?> slave,Callable.Func0<T> action){
        return hitDataSource(slave,action);
    }

    public static <T> T hitDataSource(Comparable<?> name,Callable.Func0<T> action){
        HintManager.clear();
        try(HintManager hintManager=HintManager.getInstance()){
            //解决多次调用报错
            hintManager.setDatabaseShardingValue(name);
            return action.invoke();
        }
    }
}
